package com.sunshine.student_register.ui.fragment.note;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.sunshine.student_register.R;
import com.sunshine.student_register.adapter.RecyclerAdapter;
import com.sunshine.student_register.database.register.RegisterEntity;

import java.util.ArrayList;

/**
 * @author sunshine
 * @date 2020/5/30
 * 查询当前用户所有签到记录
 */
public class NoteFragment extends Fragment {

    private NoteViewModel viewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_note, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        viewModel = new ViewModelProvider(this).get(NoteViewModel.class);

        Button load = getView().findViewById(R.id.button_load);
        final RecyclerView recyclerView = getView().findViewById(R.id.recycler);
        load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.getAllRegister().observe(getViewLifecycleOwner(), new Observer<ArrayList<RegisterEntity>>() {
                    @Override
                    public void onChanged(ArrayList<RegisterEntity> registerEntities) {
                        if (null != registerEntities || registerEntities.size() > 0){
                            recyclerView.setAdapter(new RecyclerAdapter(registerEntities));
                            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                        }
                    }
                });
            }
        });
    }
}
